﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using FrienDev.Data;

public partial class Controls_Club_PostList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    public void gv_ClubPostList_PreRender(object sender, EventArgs e)
    {
        BindData();
    }
    void BindData()
    {
        if (ClubRules.Instance.GetClubIsOpenByClubId(ClubId))
        {
            if (!IsPaging)
            {
                this.gv_ClubPostList.DataSource = ClubRules.Instance.GetCurrentUserClubTopics(ClubId);
                this.gv_ClubPostList.AllowPaging = false;
                this.gv_ClubPostList.DataBind();
                if (ClubRules.Instance.GetIsAdminOfClubByUserIdAndClubId(ClubId, UserRules.Instance.GetCurrentUserId()))
                {
                    this.gv_ClubPostList.Columns[4].Visible = true;
                }
            }
            else
            {
                this.gv_ClubPostList.DataSource = ClubRules.Instance.GetCurrentUserClubTopics(ClubId);
                this.gv_ClubPostList.DataBind();
                if (ClubRules.Instance.GetIsAdminOfClubByUserIdAndClubId(ClubId, UserRules.Instance.GetCurrentUserId()))
                {
                    this.gv_ClubPostList.Columns[4].Visible = true;
                }
            }
        }
        else
        {
            if (ClubRules.Instance.IsClubMemberByClubId(ClubId))
            {
                if (!IsPaging)
                {
                    this.gv_ClubPostList.DataSource = ClubRules.Instance.GetCurrentUserClubTopics(ClubId);
                    this.gv_ClubPostList.AllowPaging = false;
                    this.gv_ClubPostList.DataBind();
                    if (ClubRules.Instance.GetIsAdminOfClubByUserIdAndClubId(ClubId, UserRules.Instance.GetCurrentUserId()))
                    {
                        this.gv_ClubPostList.Columns[4].Visible = true;
                    }
                }
                else
                {
                    this.gv_ClubPostList.DataSource = ClubRules.Instance.GetCurrentUserClubTopics(ClubId);
                    this.gv_ClubPostList.DataBind();
                    if (ClubRules.Instance.GetIsAdminOfClubByUserIdAndClubId(ClubId, UserRules.Instance.GetCurrentUserId()))
                    {
                        this.gv_ClubPostList.Columns[4].Visible = true;
                    }
                }
            }
            else
            {
                this.pn_Privacy.Visible = true;
                this.gv_ClubPostList.Visible = false;
            }
        }
    }

    protected void gv_ClubPostList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
    }

    protected void gv_ClubPostList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string postId = e.CommandArgument.ToString();
            ClubRules.Instance.DeleteSessionPostByPostId(postId.ToString());
            BindData();
        }
    }

    public bool IsPaging { get; set; }
    public int ClubId
    {
        get
        {
            return GetQueryStringValue("clubId", 0);
        }
    }
 
    protected void gv_ClubPostList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Post post = e.Row.DataItem as Post;
            HyperLink temp_hyPostTitle = e.Row.FindControl("hy_PostTitle") as HyperLink;
            temp_hyPostTitle.Text = post.Title;
            temp_hyPostTitle.NavigateUrl = ClubRules.Instance.GetClubPostUrl(post.PostId,ClubId);
            Label temp_lblShortDate = e.Row.FindControl("lbl_ShortDate") as Label;
            temp_lblShortDate.Text = post.CreateDateTime.ToString("MM-dd");
            Label temp_lblReplyCount = e.Row.FindControl("lbl_ReplyCount") as Label;
            if (post.Viewed != null)
            {
                temp_lblReplyCount.Text = ClubRules.Instance.GetClubReplyCountAndViewed(post.PostId, post.Viewed.Value);
            }
            else
            {
                temp_lblReplyCount.Text = ClubRules.Instance.GetClubReplyCountAndViewed(post.PostId, 1);
            }
            HyperLink temp_hyAuthor = e.Row.FindControl("hy_Author") as HyperLink;
            temp_hyAuthor.Text = post.User.FullName;
            temp_hyAuthor.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(post.UserId);
            LinkButton temp_lbtDelete = e.Row.FindControl("lbt_Delete") as LinkButton;
            temp_lbtDelete.CommandArgument = post.PostId.ToString();
            if (post != null)
            {
                Post lastReplyPost = ClubRules.Instance.GetReplyPosts(post.ParentId.Value).First();
                HyperLink temp_hyReplyAuthor = e.Row.FindControl("hy_PostReplyAuthor") as HyperLink;
                temp_hyReplyAuthor.Text = lastReplyPost.User.FullName;
                temp_hyReplyAuthor.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(lastReplyPost.UserId);
                Label temp_lblReplyDateTime = e.Row.FindControl("lbl_ReplyDateTime") as Label;
                temp_lblReplyDateTime.Text = lastReplyPost.CreateDateTime.ToString("MM-dd HH:mm");
            }
        }

    }

    #region Protected Method
    protected int GetQueryStringValue(string QueryName, int nullValue)
    {

        if (string.IsNullOrEmpty(this.Page.Request.QueryString[QueryName]))
        {
            return nullValue;
        }
        return Convert.ToInt32(this.Page.Request.QueryString[QueryName]);
    }
    protected string GetQueryStringValue(string QueryName, string nullValue)
    {

        if (string.IsNullOrEmpty(this.Page.Request.QueryString[QueryName]))
        {
            return nullValue;
        }
        return this.Page.Request.QueryString[QueryName];
    }

    protected bool GetQueryStringValue(string QueryName, bool nullValue)
    {
        if (string.IsNullOrEmpty(this.Page.Request.QueryString[QueryName]))
        {
            return nullValue;
        }
        return Convert.ToBoolean(this.Page.Request.QueryString[QueryName]);
    }
    #endregion
}
